<?php
class articleApp extends core
{
    public function listAction($arg)
    {
        $websites   = base::init('website')->fetchAll();

        $template   = $this->template();
        $template->assign('websites', $websites);
        $template->display('article_list.php');
    }

    public function editAction($arg)
    {
        if (isPost())
        {
            $arg['content'] = $arg['body'];
            base::init('article')->editByid($arg['id'], $arg);
            $this->refresh('修改成功', $arg['referrer']);
        }
        else 
        {
            $template   = $this->template();
            $article    = base::init('article')->fetchRowById($arg['id']);
            $columns    = base::init('column')->fetchAll(array('wid'=>$arg['wid']));
            $columns    = base::init('column')->listcls($columns, 0);
            $column     = $columns[$article['classid']];

            $template->assign('article', $article);
            $template->assign('columns', $columns);
            $template->assign('column', $column);
            $template->display('article_edit.php');
        }
    }

    public function addAction($arg)
    {
        if (isPost())
        {
            base::init('article')->insert($arg);
            $this->refresh('添加成功', $arg['referrer']);
        }
        else 
        {
            $template   = $this->template();
            $columns    = base::init('column')->fetchAll(array('wid'=>$arg['wid']));
            $columns    = base::init('column')->listcls($columns, 0);
            $column     = $columns[$arg['classid']];
            $user       = base::init('user')->getInfo();

            $template->assign('columns', $columns);
            $template->assign('user', $user);
            $template->assign('column', $column);
            $template->display('article_add.php');
        }
    }
    
    
    public function deleteAction($arg)
    {
        if (isset($arg['ajax']))
        {
            base::init('article')->delete(array('aid'=>$arg['aid']));
            print_r('1');
        }
    }

    public function setAction($arg)
    {
        if ('html' == $arg['field'])
        {
            $where  = array('aid'=>$arg['aid']);
            $edit   = array('html'=>$arg['value']);
            base::init('article')->edit($where, $edit);
            exit('1');
        }
        else if (in_array($arg['field'], array('h', 'c', 'a')))
        {
            $value  = explode(',', $arg['value']);
            if (!strstr($arg['value'], $arg['field']))
            {
                $value[]    = $arg['field'];
            }
            else 
            {
                foreach ($value as $key => $val)
                {
                    if ($val == $arg['field']) unset($value[$key]);
                }
            }
            $value  = array_filter($value);
            $where  = array('aid'=>$arg['aid']);
            $edit   = array('flag'=>join(',',$value));
            base::init('article')->edit($where, $edit);
            exit('1');
        }
    }
}
